<?php
namespace XtwsBack\Controller;
use Think\Controller;
use XtwsBack\Model\ShopPayLogModel;

class PayLogController extends Controller{

    //扫码进入这个页面，用户在该页面输入支付金额
    public function Index(){



        $sn = I('sn');  //接收二维码传递过来的参数，格式123(店铺id)-4567(店铺老板id)
        if ($sn){
            $store_id = strstr($sn,"-",true);
            $storeInfo = M('shop_store')->where(array('id'=>$store_id))->field('id,storename,u_id')->find(); //查询店铺名称
            $this->assign('storeInfo',$storeInfo);
            $this->display();
        }else{
            $this->error('缺少参数，请检查');
        }//end if $sn

    }//end fun Index()

    // 用户输入支付金额-调用支付接口
    public function toPay(){
        if(IS_POST){
            $post = I('post.');
            $model = new ShopPayLogModel();
            // 调用支付接口
            $result = $model->pay($post);
        }else{
            $this->error('非法进入');
        }//end if IS_POST

    }//end fun toPay()


    // 支付回调通知
    public function notifyPay($sn) {
        if($sn){
            return 'success=Y';
        }else{
            return null;
        }//end if $sn

    }//end fun notifyPay();

    // 支付成功后的处理-给用户增加积分，目前来看会调用两次，不清楚原因
    public function successPay() {
        $id = intval(I('pay_id'));    //查询支付记录
        $pay_info = M('shop_pay_log')->where(array('id'=>$id))->find();
        if($pay_info){
            if($pay_info['status']==0){
                if($pay_info['consumer_id']!=''){//微信用户支付，增加积分
                    M('member')->where(array('openid'=>$pay_info['consumer_id']))->setInc('credit1',$pay_info['amount']);
                }
                // 更新支付状态
                $pay_info['status'] = 1;
                $pay_info['updated_at'] = time();
                M('shop_pay_log')->save($pay_info);

                // 给店铺老板推送消息
                $send = sendMsg($id);

                // 跳转回首页
                $this->redirect('http://www.xiangtws.com/Wx/');
            }else{
                $this->error('已经更新过记录啦');
            }
        } // end if $pay_info

    }//end fun successPay();






}//end class